package leetcode.code0890;

import java.util.ArrayList;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

public class Solution implements IDebug {

	public List<String> findAndReplacePattern(String[] words, String pattern) {
		int len = pattern.length();
		char[] p = pattern.toCharArray();
		List<String> ans = new ArrayList<>();
		char tmp = 0, ti = 0;
		for (String word : words) {
			search: if (word.length() == len) {
				int[] dic = new int[128];
				boolean[] used = new boolean[128];
				for (int i = 0; i < len; i++) {
					ti = word.charAt(i);
					if (dic[tmp = p[i]] == 0) {
						if (used[ti]) {
							break search;
						}
						dic[tmp] = ti;
						used[ti] = true;
					} else if (dic[tmp] != ti) {
						break search;
					}
				}
				ans.add(word);
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		List<String> e = HelpDebug.str2liststr("[\"mee\",\"aqq\"]");
		HelpDebug.compare(e,
				this.findAndReplacePattern(HelpDebug.str2array2("[\"abc\",\"deq\",\"mee\",\"aqq\",\"dkd\",\"ccc\"]"), "abb"));

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
